package fr.ensisa.hassenforder.game.client;

import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collection;

import fr.ensisa.hassenforder.game.model.Player;
import fr.ensisa.hassenforder.game.model.Product;
import fr.ensisa.hassenforder.network.FileHelper;
import fr.ensisa.hassenforder.network.Protocol;

public class SessionClient {

	private Socket connection;
	private String name;
	private long id;

	public SessionClient(Socket connection) {
		this.connection = connection;
		this.name = null;
		this.id = -1;
	}

	public boolean connect(String username, String userpassword) {
		try {
			Writer w = new Writer(connection.getOutputStream());
			Reader r = new Reader(connection.getInputStream());
			w.createConnection(username, userpassword);
			w.send();
			r.receive();
			if (r.getParams() != null) {
				this.name = (String) r.getParams().get(0);
				this.id = (long) r.getParams().get(1);
				System.out.println(name + "  " + id);
				return true;
			} else
				connection.close();
		} catch (IOException e) {
			return false;
		}
		return false;
	}

	public boolean disconnect() {
		try {
			Writer w = new Writer(connection.getOutputStream());
			Reader r = new Reader(connection.getInputStream());
			w.createDisconnection(this.name, this.id);
			w.send();
			r.receive();

			return (boolean) r.getdisconnect().get(0);
		} catch (IOException e) {
			return false;
		}

	}

	public boolean addCash(int amount) {
		try {
			Writer w = new Writer(connection.getOutputStream());
			Reader r = new Reader(connection.getInputStream());
			w.createAddCash(this.name, this.id, amount);
			w.send();
			r.receive();
			return (boolean) r.getParams().get(0);
		} catch (IOException e) {
			return false;
		}
	}

	public boolean clearProducts() {
		try {
			Writer w = new Writer(connection.getOutputStream());
			Reader r = new Reader(connection.getInputStream());
			w.creatClear(this.name, this.id);
			w.send();
			r.receive();
			return (boolean) r.getParams().get(0);
		} catch (IOException e) {
			return false;
		}
	}

	public boolean consumeProducts() {
		try {
			Writer w = new Writer(connection.getOutputStream());
			Reader r = new Reader(connection.getInputStream());
			w.creatConsume(this.name, this.id);
			w.send();
			r.receive();
			return (boolean) r.getParams().get(0);
		} catch (IOException e) {
			return false;
		}
	}

	public Player getStatistics() {
		try {
			Writer w = new Writer(connection.getOutputStream());
			Reader r = new Reader(connection.getInputStream());
			w.createGetStatistique(this.name, this.id);
			w.send();
			r.receive();
			return new Player(this.name, (String) r.getParams().get(1), (int) r
					.getParams().get(0));
		} catch (IOException e) {
			return null;
		}
	}

	public Collection<Product> getProducts() {
		try {
			Writer w = new Writer(connection.getOutputStream());
			Reader r = new Reader(connection.getInputStream());
			w.creatGetProducts(name, id);
			w.send();
			r.receive();
			return (ArrayList<Product>) r.getParams().get(0);
		} catch (IOException e) {
			return null;
		}
	}

	public Collection<Product> getShop() {
		try {
			Writer w = new Writer(connection.getOutputStream());
			Reader r = new Reader(connection.getInputStream());
			w.creatGetShop(name, id);
			w.send();
			r.receive();
			return (ArrayList<Product>) r.getParams().get(0);
		} catch (IOException e) {
			return null;
		}
	}

	public boolean refreshShop() {
		try {
			Writer w = new Writer(connection.getOutputStream());
			Reader r = new Reader(connection.getInputStream());
			w.creatRefreshShops(name, id);
			w.send();
			r.receive();
			return (boolean) r.getParams().get(0);
		} catch (IOException e) {
			return false;
		}
	}

	public boolean buyProduct(String productName) {
		try {
			Writer w = new Writer(connection.getOutputStream());
			Reader r = new Reader(connection.getInputStream());
			w.creatBuy(name, id, productName);
			w.send();
			r.receive();
			return (boolean) r.getParams().get(0);
		} catch (IOException e) {
			return false;
		}

	}

	public boolean sellProduct(String productName) {
		try {
			Writer w = new Writer(connection.getOutputStream());
			Reader r = new Reader(connection.getInputStream());
			w.creatSell(name, id, productName);
			w.send();
			r.receive();
			return (boolean) r.getParams().get(0);
		} catch (IOException e) {
			return false;
		}
	}

	public String getImage(String imageName) {
		try {
			Writer w = new Writer(connection.getOutputStream());
			Reader r = new Reader(connection.getInputStream());
			w.creatGetImage(imageName);
			w.send();
			r.receive();
			return (String) r.getParams().get(0);
		} catch (IOException e) {
			return null;
		}
	}
}
